{% extends "base/micro.html" %}

{% block title %}
Service Delete | SwarmOps - Docker管理平台
{% endblock %}

{% block head %}
    <!-- Import chosen css -->
    <link rel="stylesheet" href="{{ url_for('ui.static', filename='css/chosen.min.css') }}">
{% endblock %}

{% block container %}
<form class="form-horizontal" role="form" id="DelServiceForm">

  <div class="form-group">
      <label for="DelServiceflag" class="col-sm-12">
          <p class="text-success "><span class="glyphicon glyphicon-arrow-down"></span>服务标识名称列表<scan class="badge"> {{ g.service.GET().data|length }}</scan><span class="glyphicon glyphicon-arrow-down"></span></p>
      </label>
      <div class="col-sm-12">
          <select id="DelServiceflag" data-placeholder="选择一个或多个服务删除" class="chosen-select" multiple tabindex="4" style="width: 100%">
          {% for service in g.service.GET(core=True, core_convert=True).data %}
              <option value="{{ service.ID }}">{{ service.Name }} &lt;{{ service.ID }}&gt;</option>
          {% endfor %}
          </select>
      </div>
  </div>

  <div class="form-group">
    <div class=" col-sm-12">
      <button type="button" onclick=DelService() class="btn btn-danger">批量删除服务</button>
    </div>
  </div>

</form>
{% endblock %}
{% block script %}

<!-- Import chosen js -->
<script src="{{ url_for('ui.static', filename='js/chosen.jquery.min.js') }}" type="text/javascript"></script>
<script type="text/javascript">
// chosen config
var config = {
    '.chosen-select'           : {},
    '.chosen-select-deselect'  : {allow_single_deselect:true},
    '.chosen-select-no-single' : {disable_search_threshold:10},
    '.chosen-select-no-results': {no_results_text:'Oops, nothing found!'},
    '.chosen-select-width'     : {width:"95%"}
}
for (var selector in config) {
    $(selector).chosen(config[selector]);
}
function DelService() {
  //var delService = $("input#DelServiceflag").val()
  var delService = $('#DelServiceflag').val();
  console.log(delService);
  if (delService) {
    if(window.confirm("你确定要删除" + delService +"?")) {
        //var services = delService.split(" ");
        var services = delService;
        var SUCCESS  = new Array();
        var FAILED   = new Array();
        console.log(services);
        for (i=0;i<services.length ;i++ ) {
            flag = services[i];
            if (flag) {
                $.ajax({
                    url: "{{ url_for('apis.core.service') }}",
                    method: 'DELETE',
                    async: false,
                    datatype: 'json',
                    data: {"flag": flag},
                    success: function(res) {
                        console.log(res);
                        if (res.success==true) {
                            console.log("Delete Service Successfully:" + flag);
                            SUCCESS.push(flag);
                        } else {
                            console.log("Delete Service Failed:" + flag);
                            FAILED.push(flag);
                        }
                    },
                });
            }
        }
        if (SUCCESS.length!==0) {
            alert("服务删除成功列表：" + SUCCESS);
        }
        if (FAILED.length!==0) {
            alert("服务删除失败列表：" + FAILED);
        }
        //window.location.href="{{ url_for('ui.service') }}";
        parent.location.reload();
    }
  } else {
      alert("请填写要删除的服务标识！");
  }
}
</script>
{% endblock %}
